package com.web.account.controller;

import com.web.account.entity.Account;
import com.web.account.service.AccountService;
import com.web.common.BaseResult;
import com.web.common.enums.BaseStatus;
import com.web.common.enums.PendingAction;
import com.web.customer.entity.Customer;
import com.web.customer.service.CustomerService;
import com.web.pendingList.service.PendingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wenzhuo
 * @create 2024-01-09 2024/1/9
 */

@RestController
@CrossOrigin
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private PendingListService pendingService;

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "/getAccountList")
    public BaseResult<List<Account>> getCustomerList(@ModelAttribute Account account){
        List<Account> accountList = accountService.getAccountList(account.getAccountNo(), account.getCurrency(), account.getCustomerNo());

        return new BaseResult(BaseStatus.SUCCESS.getCode(),accountList);
    }


    @PostMapping(value = "/addAccount")
    public BaseResult addAccount(@ModelAttribute Account account,String userName){
        //判断客户是否存在
        if(customerService.getCustomerList(account.getCustomerNo(),null,null).size() == 0){
            return new BaseResult<>(BaseStatus.CUSTOMER_NOT_EXIST.getCode(),BaseStatus.CUSTOMER_NOT_EXIST.getDesc());
        }

        //新增账户时判断是否已经存在
        if(!accountService.checkData(account).isEmpty()){
            return new BaseResult<>(BaseStatus.ACCOUNT_ALREADY_EXIST.getCode(),BaseStatus.ACCOUNT_ALREADY_EXIST.getDesc());
        };

        //新增Pending数据
       return pendingService.addAccountPending(account, userName,Account.MODULE,PendingAction.ADD.getAction());
    };

    @PostMapping(value = "/updateAccount")
    public BaseResult updateCustomer(@ModelAttribute Account account,String userName){

        //查询出还未修改的Account数据.
        List<Account> accountList = accountService.getAccountList(account.getAccountNo(), account.getCurrency(), account.getCustomerNo());
        if(accountList.size() != 1){
            return new BaseResult<>(BaseStatus.ACCOUNT_DATA_EXCEPTION.getCode(),BaseStatus.ACCOUNT_DATA_EXCEPTION.getDesc());
        }
        Account accountOld = accountList.get(0);
        //新增修改审批相关的数据
        pendingService.handleModifyAccount(accountOld,account,userName);

        return new BaseResult<>(BaseStatus.SUCCESS.getCode(),BaseStatus.FCE_UPDATE_SUCCESS.getDesc());
    };

    @PostMapping(value = "/deleteAccount")
    public BaseResult deleteCustomer(@RequestParam("accountNo") String accountNo,String userName){
        //根据accountNo查询出Account
        Account account = accountService.getAccountList(accountNo, null, null).get(0);

        //新增Pending记录获取PendingNumber并且将PendingNumber赋值给Account
        return pendingService.addAccountPending(account, userName,Account.MODULE, PendingAction.DEL.getAction());

    };

    @GetMapping(value = "/getAccount")
    public BaseResult<List<Account>> getAccountNoByCustomer(@ModelAttribute Account account){
        List<Account> accountWithCustomer = accountService.getAccountWithCustomer(account);
        if(accountWithCustomer.size() != 1){
            return new BaseResult<>(BaseStatus.ACCOUNT_DATA_EXCEPTION.getCode(),BaseStatus.ACCOUNT_DATA_EXCEPTION.getDesc());
        }
        return new BaseResult<>(BaseStatus.SUCCESS.getCode(),accountWithCustomer);
    }

}
